package com.example.chatzsyproject.base;

import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.RelativeLayout;
import android.widget.Toast;

import androidx.annotation.Nullable;
import androidx.fragment.app.Fragment;

import com.example.chatzsyproject.R;

/**
 * Created by Administrator on 2017/9/19.
 */

public class BaseFragment extends Fragment {


    private Toast mToast = null;


    private View failedView = null;
    private OnFailReloadLinstener linstener = null;


    @Nullable
    @Override
    public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
        mToast = Toast.makeText(getActivity(), "", Toast.LENGTH_SHORT);
        return super.onCreateView(inflater, container, savedInstanceState);
    }


    protected void addFailedView(View view) {
        if (failedView == null) {
            ViewGroup vg = view.findViewById(R.id.content);
            failedView = getActivity().getLayoutInflater().inflate(
                    R.layout.network_failed_view, null);
            RelativeLayout.LayoutParams params = new RelativeLayout.LayoutParams(
                    ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.MATCH_PARENT);
            params.addRule(RelativeLayout.BELOW, R.id.header);
            vg.addView(failedView, params);
            failedView.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    if (linstener != null) {
                        linstener.OnFailReload();
                    }
                }
            });
        }
    }

    protected void setFailReloadListener(OnFailReloadLinstener linstener) {
        this.linstener = linstener;
    }

    public interface OnFailReloadLinstener {
        void OnFailReload();
    }

    protected void removeFailedView(View view) {
        if (failedView != null) {
            ViewGroup vg = view.findViewById(R.id.content);
            vg.removeView(failedView);
            failedView = null;
        }
    }
}
